#ifndef PLAYER_H
#define PLAYER_H

#include <QGraphicsItem>
#include <QObject>
#include <QPainter>
#include <QKeyEvent>
#include "mouse.h"
#include "map.h"
#include "movingobject.h"
#include "gun.h"
#include "bullet.h"
#include "connectionplayer.h"
#include "clientconnection.h"

class Map;
class Gun;
class MovingObject;
class Bullet;
class ConnectionPlayer;
class ClientConnection;

class Player : public MovingObject
{
    Q_OBJECT
public:
    enum { Type = UserType + 1 };
    int type() const
    {
        return Type;
    }

    QList<short > dirs;
    bool getIsAlive() const
    {
        return isAlive;
    }
    bool getIsRemote() const
	{
		return isRemote;
	}
    void setIsRemote(bool r);

    Player(qreal x, qreal y);
    ~Player();
    QRectF boundingRect() const;
    QPainterPath shape() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void changePos(QPointF p);
    void changePos(qreal x, qreal y);
    void changeXPos(qreal x);
    void changeYPos(qreal y);
    bool movePlayer();
    bool movePlayerBy(qreal dx, qreal dy);
    bool movePlayerBy(QLineF vector);
    void decreaseHealth(qreal h);
    void setPlayerAngle(qreal ang);
    qreal getRadius();
    void updateKeys(int nr);
    void setMouse(Mouse *mouse);
    void setGun(Gun *gun);
    void unsetMouse();

    ConnectionPlayer* getConnectionPlayer();
    ClientConnection* getClientConnection();

    void setConnectionPlayer(ConnectionPlayer* cp);
    void setClientConnection(ClientConnection* cc);

    quint32 getDeathTime();
    void setDeathTime(quint32 dt);

    Map *map();
    QVector<Bullet*> getBullets();
protected:
    void keyPressEvent(QKeyEvent *evt);
    void keyReleaseEvent(QKeyEvent *evt);
private:
    quint32 deathTime;
    ClientConnection* clientConnection;
    ConnectionPlayer* connectionPlayer;
    bool usesMouse;
    bool isAlive;
    bool isRemote;
    Mouse *mouse;
    Gun *gun;
    qreal angle;
    qreal speed;
    qreal health;
    qreal radius;
    qreal wsp_m;
    int keyToNr(int key);
    void aimAtMouse();

public slots:
    void onMousePressed(QMouseEvent *evt);
    void onMouseReleased(QMouseEvent *evt);
    void onGunFired(Bullet *b);
    void setIsAlive(bool isAlive);

private slots:
	void fireGun(qreal x, qreal y, qreal angle);
	void setPosition(QPointF pos);
	void setIsReloading(bool isReloading);
	void setAngle(qint16 angle);
	void setDirs(quint8, quint8);
	void onHitPlayer(Player*, Player*);

signals:
	void hitPlayer(Player*, Player*);


};

#endif // PLAYER_H
